package xkp.lesson5.newwork;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.ServerSocket;
import java.nio.ByteBuffer;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;

public class Server {
    public static void main(String[] args) throws IOException {
        //ServerSocket serverSocket = new ServerSocket(9999);
        //创建通道
        ServerSocketChannel serverSocketChannel = ServerSocketChannel.open();
        //监听端口
        serverSocketChannel.bind(new InetSocketAddress(9999));
        //等待连接
        SocketChannel socketChannel =serverSocketChannel.accept();
        System.out.println(socketChannel.getRemoteAddress()+":连接成功");
        ByteBuffer buffer = ByteBuffer.allocate(1024);
        int length = socketChannel.read(buffer);
        buffer.flip();
        String str = new String(buffer.array(),0,length);
        System.out.println(str);
        socketChannel.close();
    }
}
